import { defineStore } from 'pinia'
import { proGetList, proGetById, proPull, proDel,processDel, proEdit, proLogin } from "../../api/programs/index"
import { editProRequestData } from "../../api/programs/types"
interface State {
  programList: programTitles[],
  editProgram: editProRequestData,
  nowProgram: editProRequestData,
  saveProgram: editProRequestData,


}

interface programTitles {
  program_id: string,
  title: string
}
export const ProgramStore = defineStore('pro', {
  //属性
  state: (): State => {
    return {
      programList: [],

      nowProgram: {
        program_id: '',
        title: '',
        introduction: " ",
        main_img: "",
        process_list: []
      },

      editProgram: {
        program_id: '',
        title: '',
        introduction: " ",
        main_img: "",
        process_list: []
      },

      saveProgram: {
        program_id: '',
        title: '',
        introduction: " ",
        main_img: "",
        process_list: []
      },



    }
  },
  //数据处理
  getters: {


  },
  //函数
  actions: {
    // 获取项目列表
    async getList() {
      this.programList = await proGetList()
    },
    // 获取项目
    async getById(id: string) {
      this.nowProgram = await proGetById({ "program_id": id })
    },
    // 上传项目
    async pullPro() {
      await proPull(this.editProgram)
    },
    // 登陆
    async login(data: any) {
      await proLogin(data)

    },
    // 删除
    async delPro(data: any) {
      await proDel(data)

    },

    async delProcess(data: any){
      await processDel(data)
    },
    
    // 修改
    async editPro() {
      await proEdit(this.editProgram)
    },






  },
  // 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        // 自定义名称
        key: 'pro',
        // 保存位置，默认保存在sessionStorage
        storage: localStorage,
        // 指定要持久化的数据，默认所有 state 都会进行缓存，你可以通过 paths 指定要持久化的字段，其他的则不会进行持久化。
        paths: ['nowProgram', 'programList', 'editProgram', 'saveProgram'],
      },
    ],
  },

})